-- 客机框选区域已完成

-- 记录起点终点
-- 将起点终点转换为世界坐标发送到服务器
-- 判断单位是否在范围内
-- 区域内的单位高亮
-- 执行行为逻辑

local HT = require('core_alpha_rts/main/select_handle_tools')
local SelectHandler = require('core_alpha_rts/main/select_handler')

local Image = require('widgets/image')

local function tkr()
    return Image('images/global.xml','square.tex')
end


-- 鼠标左键按下时 框选区域
local isdown = false
local start_x,start_y
TheInput:AddMouseButtonHandler(function(button, down, x, y)
    if TheInput:IsKeyDown(KEY_CTRL) then
        return
    end
    if ThePlayer and ThePlayer.HUD and ThePlayer.HUD.alpha_rts_hint then
        if button == 1001 then
            if down and not isdown then
                isdown = true
                if not ThePlayer.HUD.alpha_rts_hint.shown then
                    ThePlayer.HUD.alpha_rts_hint:Show()
                end
                ThePlayer.HUD.alpha_rts_hint:SetPosition(x,y)
                start_x,start_y = x,y
            end
            if not down and isdown then
                -- 发送坐标
                if start_x and start_y and x and y then
                    -- 获取屏幕的矩形顶点坐标 转 世界坐标
                    local world_pos_a_x,_,world_pos_a_z = TheSim:ProjectScreenPos(start_x,start_y)
                    local world_pos_b_x,_,world_pos_b_z = TheSim:ProjectScreenPos(x,y)
                    local world_pos_c_x,_,world_pos_c_z = TheSim:ProjectScreenPos(start_x,y)
                    local world_pos_d_x,_,world_pos_d_z = TheSim:ProjectScreenPos(x, start_y)
                    -- SendModRPCToServer(MOD_RPC['alpha_rts']["send_area_pos"],start_x,start_y,x,y)
                    SendModRPCToServer(MOD_RPC['alpha_rts']["send_area_pos"],world_pos_a_x,world_pos_a_z,world_pos_b_x,world_pos_b_z,world_pos_c_x,world_pos_c_z,world_pos_d_x,world_pos_d_z)
                end
                isdown = false
                ThePlayer.HUD.alpha_rts_hint:Hide()
                ThePlayer.HUD.alpha_rts_hint:SetSize(0,0)
                start_x,start_y = nil,nil
            end
        end
    end
end)

-- 指针移动时 框选区域 自适应缩放
TheInput:AddMoveHandler(function(x,y)
    if isdown then
        if ThePlayer and ThePlayer.HUD and ThePlayer.HUD.alpha_rts_hint and start_x and start_y then
            ThePlayer.HUD.alpha_rts_hint:SetSize(x-start_x,start_y-y)
        end
    end
end)

-- playerhud 添加 框选区域 矩形
AddClassPostConstruct("screens/playerhud", function(self)
    ---@type widget_image
    self.alpha_rts_hint = self:AddChild(tkr())
    self.alpha_rts_hint:SetVRegPoint(ANCHOR_TOP)
    self.alpha_rts_hint:SetHRegPoint(ANCHOR_LEFT)
    self.alpha_rts_hint:SetSize(0,0)
    self.alpha_rts_hint:SetTint(1,1,1,0.5)

    self.alpha_rts_hint:Hide()
end)



---@class ent
---@field alpha_rts_selected_ent nil|ent_alpha_rts[] # 右键选择的单位

-- 注册 框选 RPC
AddModRPCHandler("alpha_rts","send_area_pos", function(player,world_pos_a_x,world_pos_a_z,world_pos_b_x,world_pos_b_z,world_pos_c_x,world_pos_c_z,world_pos_d_x,world_pos_d_z)

    -- 重置之前的 框选单位
    SelectHandler:ResetSelectUnits(player)

    local center_x,center_z = (world_pos_a_x+world_pos_b_x)/2,(world_pos_a_z+world_pos_b_z)/2

    local radius = C_ALPHA_RTS:calcDist(world_pos_a_x,world_pos_a_z,center_x,center_z,true)
    local ents = TheSim:FindEntities(center_x, 0, center_z, radius, {'_combat','_health'}, {'wall',"structure",'player'})
    for _,ent in ipairs(ents) do

        -- 对框选目标进行筛选
        if HT:SelectUnitFilter(ent) then
            if S_ALPHA_RTS:checkAlive(ent) then
                local tar_x,_,tar_z = ent.Transform:GetWorldPosition()

                -- 在框选区域中
                if C_ALPHA_RTS:isPointInPolygon({tar_x,tar_z},{world_pos_a_x,world_pos_a_z},{world_pos_c_x,world_pos_c_z},{world_pos_b_x,world_pos_b_z},{world_pos_d_x,world_pos_d_z}) then
                    ent.AnimState:SetMultColour(1,.5,.5,1)
                    table.insert(player.alpha_rts_selected_ent,ent)

                    -- 挂上特殊属性表
                    if ent.alpha_rts_selected_ent_attri == nil then
                        ---@diagnostic disable-next-line: inject-field
                        ent.alpha_rts_selected_ent_attri = {}
                    end

                    local PhysicsRadius = ent:GetPhysicsRadius(0)
                    local circle_scale = PhysicsRadius*HT.PhysicsRadiusToHightLightCircle_FixFactor
                    ent.alpha_rts_selected_ent_attri.highlight_circle = SpawnPrefab('alpha_rts_circle')
                    ent.alpha_rts_selected_ent_attri.highlight_circle.entity:SetParent(ent.entity)
                    ent.alpha_rts_selected_ent_attri.highlight_circle.entity:AddFollower()
                    ent.alpha_rts_selected_ent_attri.highlight_circle.Follower:FollowSymbol(ent.GUID, nil, 0, 0, 0)
                    ent.alpha_rts_selected_ent_attri.highlight_circle.AnimState:SetMultColour(98/255,218/255,61/255,1)
                    ent.alpha_rts_selected_ent_attri.highlight_circle.Transform:SetScale(circle_scale,circle_scale,circle_scale)
                end
            end
        end
    end
end)